#tst batch
load('lik_theta_list.txt')
mat.pooled <- matrix(0,ncol=10,nrow=nrow(lik.theta.list[[1]]$BBlikTest.pooled.df))

for(i in 1:10){
	tmp.lik <- lik.theta.list[[1]]$lik.theta.fit
	tmp.lik <- tmp.lik[sample(1:nrow(tmp.lik)),]
	tmp.agg <- aggregate(tmp.lik,by=list(lik.theta.list[[1]]$cDNA),sum)
	
	tmp.pooled <- data.frame(cDNA=tmp.agg[,1],null.lik = tmp.agg[,lik.theta.list[[1]]$null.which+1],
	max.lik = rep(0,nrow(tmp.agg)))
	tmp.pooled$cDNA <- as.character(tmp.pooled$cDNA)
	
	tmp.pooled$max.lik <- apply(tmp.agg[,-1],1,max)
	tmp.pooled$chi.sq <- 2*(tmp.pooled$max.lik-tmp.pooled$null.lik)
	tmp.pooled$p.val <- 1-pchisq(tmp.pooled$chi.sq,1)
	mat.pooled[,i] <- tmp.pooled$chi.sq
	if((i %% 2) ==0) cat(i,'iterations\n')
	}

tst.out <- data.frame(cDNA=tmp.pooled[,1],stringsAsFactors=FALSE)
tst.out <- cbind(tst.out,t(apply(X=mat.pooled,1,function(X){quantile(X,c(0.9,0.95,0.99,0.999))})))
save(tst.out,file='tst_out.txt',ascii=TRUE)